﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class DataSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["skole"] == null)//Da se ne bi pozvalo kada se pritisne neko dugme npr. delete
            {
                var connection = new System.Data.SqlClient.SqlConnection();
                connection.ConnectionString = @"Data Source=.\Katmai;Initial Catalog=Test;Integrated Security=True";
                var sqlCommand = connection.CreateCommand();
                sqlCommand.CommandText = "SELECT TOP 2 * FROM Skola";

                var dataset = new System.Data.DataSet();
                var adapter = new System.Data.SqlClient.SqlDataAdapter();
                adapter.SelectCommand = sqlCommand;
                adapter.Fill(dataset);
                dgSkole.DataSource = dataset;
                dgSkole.DataBind();


                Session["skole"] = dataset;//sacuvam ga za kasnije
                Session["adapter"] = adapter;
            }
            else
            {
                var dataset = Session["skole"] as System.Data.DataSet;
                dgSkole.DataSource = dataset;
                dgSkole.DataBind();
            }

            
        }

        /// <summary>
        /// Pozove se kada se delete dugme pritisne
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dgSkole_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            /*
            var dataset = Session["skole"] as System.Data.DataSet;
            dataset.Tables[0].Rows[e.RowIndex].Delete();
            dataset.AcceptChanges();
            dgSkole.DataBind();
             * */
            var dataset = Session["skole"] as System.Data.DataSet;
            var result = "";

            for(int i = 0; i<dataset.Tables[0].Rows.Count; i++)
            {
                result = result + 
                        Convert.ToString(dataset.Tables[0].Rows[i]["Naziv"]) + "," + Convert.ToString(dataset.Tables[0].Rows[i]["Adresa"]) + "\n";
            }

            Response.Write(result);
            var file = new System.IO.FileStream("C:\\test.csv", System.IO.FileMode.CreateNew);
            file.Write(

            Response.End();


        }
    }
}